<?php
	
	define('HOST','localhost');
	define('USER','root');
	define('PWD','');
	define('DB','pass');
	define('CHARSET','utf8');
	

	class Model
	{
		//成员属性
		protected $tableName;
		protected $link;
		protected $pk;
		protected $fields = array();

		protected $where;
		protected $limit;
		protected $order;
		protected $field;



		//构造方法
		public function __construct($tableName)
		{
			$this->tableName = $tableName;

			//连接数据库
			$this->link = mysqli_connect(HOST,USER,PWD);
			//选择数据库
			mysqli_select_db($this->link,DB);
			//设置字符集
			mysqli_set_charset($this->link,CHARSET);
			//获取有效字段
			$this->getField();
		}

		/**
		*	添加的方法
		*	array('username'=>'werwe','password'=>'234','age'=>'23')
		*
		*	insert into user(username,password,age) values ('wew','123','23')
		*/
		public function add($arr)
		{
			//遍历有效字段
			foreach ($arr as $k => $v) {
				//筛选有效字段
				if(!in_array($k,$this->fields)) {
					//删除非法字段
					unset($arr[$k]);
				}
			}
			//准备sql语句
			$sql = "insert into {$this->tableName}";
			//获取数组的下标
			$keys = array_keys($arr);
			//获取字段
			$key_str = "(".implode(',',$keys).")";
			//获取数组的值
			$values = array_values($arr);
			//获取字段的值
			$value_str = "('".implode("','",$values)."')";
			//拼接sql语句
			$sql = $sql.$key_str.' values '.$value_str;
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				return mysqli_insert_id($this->link);
			} else {
				return false;
			}			
		}

		/**
		*	获取有效字段
		*/

		public function getField()
		{
			//准备sql语句
			$sql = 'desc '.$this->tableName;
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				while($row = mysqli_fetch_assoc($result)) {

					if($row['Key'] == 'PRI') {

						$this->pk = $row['Field'];
					} else {

						$this->fields[] = $row['Field']; 
					}
				}
			}

		}

		/**
		*	删除方法
		**/
		public function delete($id)
		{
			//准备sql语句
			$sql = "delete from {$this->tableName} where id = {$id}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				return mysqli_affected_rows($this->link);
			} else {

				return false;
			}
		}

		/**
		*	修改方法
		*
		*	array('username'=>'werwe','password'=>'234','age'=>'23','id'=>23)
		*	
		*	update user set username='wewr',password='234','age'='34' where id = 34;
		*/
		public function update($arr)
		{
			foreach ($arr as $k => $v) {
				if(!in_array($k,$this->fields) && $k != $this->pk) {

					unset($arr[$k]);
				}
			}
			//准备sql语句
			$sql = "update {$this->tableName} set ";

			//声明变量
			$where = '';
			$str = '';
			//遍历
			foreach ($arr as $k => $v) {

				if($k != $this->pk) {

					$str .= $k.'='."'$v'".',';

				} else {

					$where = " where {$this->pk} = $v";
				}
				
			}

			$strs = rtrim($str,',');
			if(empty($where))
			{
				die('修改条件不符合要求,请检查');
			}
			//拼接sql语句
			$sql  = $sql.$strs.$where;
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				return mysqli_affected_rows($this->link);
			} else {
				return false;
			}
		}

		/**
		*	where 条件
		**/
		public function where($where)
		{
			$this->where = $where;
			return $this;
		}

		/**
		*	limit 条件
		*/
		public function limit($limit)
		{
			$this->limit = $limit;
			return $this;
		}

		/**
		*	order by 条件
		*
		*/
		public function order($order)
		{
			$this->order = $order;
			return $this;
		}

		/**
		*	field 条件	
		*/

		public function field($field)
		{
			$this->field = $field;
			return $this;
		}

		/**
		*	查询方法   where  order by limit  field
		*/

		public function select()
		{
			//查询条件
			$where_str = '';
			if(!empty($this->where)) {

				$where_str = "where ".$this->where;
			}

			//limit条件
			$limit_str = '';
			if(!empty($this->limit)) {

				$limit_str = 'limit '.$this->limit;
			}

			//order条件
			$order_str = '';
			if(!empty($this->order)) {

				$order_str = 'order by '.$this->order;
			}

			//field 条件
			$field_str = '';
			if(!empty($this->field)) {

				$field_str = $this->field;
			} else {
				$field_str = "*";
			}

			//准备sql语句
			$sql = "select {$field_str} from {$this->tableName} {$where_str} {$order_str} {$limit_str}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);

			//清空属性
			$this->where = '';
			$this->limit = '';
			$this->order = '';
			$this->field = '';

			if($result) {

				$arr = array();

				while($row = mysqli_fetch_assoc($result)) {

					$arr[] = $row;
				}

				return $arr;

			} else {

				return false;
			}
		}

		//查询单条
		public function find($id)
		{
			//准备sql语句
			$sql = "select * from {$this->tableName} where id = {$id}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				$row = mysqli_fetch_assoc($result);

				return $row;
			} else {
				return false;
			}

		}

		//查询总数据
		public function count()
		{
			//准备sql语句
			$sql = "select count(*) as total from {$this->tableName}";
			//执行sql语句
			$result = mysqli_query($this->link,$sql);
			if($result) {

				while($row = mysqli_fetch_assoc($result)) {
					
					return $row['total'];
				}
			} else {

				return false;
			}
		}
	}


	//实例化对象
	//$mod = new Model('user');

	//$mod->getField();

	//添加数据

	// $arr = array('username'=>'werwe','password'=>'234','age'=>'23','abc'=>'acd');

	// var_dump($mod->add($arr));

	
	//获取有效字段
	// echo $mod->pk;

	// var_dump($mod->fields);

	//var_dump($mod);

	//删除数据
	//var_dump($mod->delete(25));


	//修改数据
	// $arr = array('username'=>'werwe','password'=>'234','age'=>'23','id'=>19,'abc'=>'anc');

	// var_dump($mod->update($arr));


	//查询多条数据
	// $mod->where('id >10')->order('id desc')->select();

	//查询单条数据
	// var_dump($mod->find(13));

	//查询总数据
	//var_dump($mod->count());
